import sys
import sqlite3
import musicbrainzngs as mb
from utils import dbconn, update_mbrelease

conn = dbconn()
cursor = conn.cursor()

response = raw_input("delete all entries in table mbrelease and reload? (y/N) > ")
if response.lower() != "y" :
	sys.exit()
	
conn.execute("delete from mbtrack")
conn.execute("delete from mbrelease")

# Clear out the musicbrainz release "cache"
## see: https://code.google.com/p/didlio/wiki/Schema
# there might be other sources of releaseid (eg. encoded tracks)
releases = list(cursor.execute("select distinct release from disc"))
#n = cursor.execute("select count(*) from (select distinct release from disc)").next()[0]
n = len(releases)

for (i, (release,)) in enumerate(releases) :
	update_mbrelease(conn, release)
	sys.stdout.write("\r%4d releases to go... " % (n-i-1))
	sys.stdout.flush()

response = raw_input("commit the updates (and delete the old data)? (y/N) > ")
if response.lower() == "y" :
	conn.commit()

sys.stdout.write("\r\n")
